package au.org.arcs.dss.provider;

import java.util.*;

import au.org.arcs.dss.provider.dataset.DataSet;
import au.org.arcs.dss.provider.dataset.facade.DataSetFacade;
import au.org.arcs.dss.provider.dataset.request.DataSetRequest;
import au.org.arcs.dss.provider.dataset.response.DataSetResponse;
import au.org.arcs.dss.provider.util.SystemUtils;

/**
 *
 * @author ming
 */
public class DataSetServiceDelegate {
  private static DataSetFacade facade;

  public DataSetServiceDelegate() {
    facade = DataSetServiceLocator.getInstance().getDataSetFacade();
  }

  public DataSet getDescendant(DataSet parent, String childPath) {
    DataSetRequest request = new DataSetRequest();

    if (childPath == null || childPath.equals("")) {
      request.setPathContainsName(true);
    }

    request.setName(SystemUtils.normalizeName(childPath));
    request.setPath(SystemUtils.normalizePath(parent.getPath()));
    request.setLoadDescendent(false);
    request.setLoadParent(false);
    request.setLoadData(true);

    DataSetResponse response = facade.getDataSets(request);

    return response.getDataSets().size() > 0 ? response.getDataSets().get(0) : null;
  }

  public List<DataSet> getDataSets(DataSet parent) {
    DataSetRequest request = new DataSetRequest();

    request.setPath(parent.getPath());
    request.setLoadData(false);

    DataSetResponse response = facade.getDataSets(request);

    return response.getDataSets();
  }

  public DataSet getDataSet(String path) {
    DataSetRequest request = new DataSetRequest();
    request.setName("");
    request.setPath(path);
    request.setLoadData(true);
    request.setPathContainsName(true);
    request.setCheckForUpdate(true);

    DataSetResponse response = facade.getDataSets(request);
   
    return response.getDataSets().size() > 0 ? response.getDataSets().get(0) : null;
  }
}